package com.yunji.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunji.news.entity.Major;
import com.yunji.news.mapper.MajorMapper;
import com.yunji.news.service.MajorService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 专业信息服务实现类
 *
 * @author yunji
 * @since 2024-11-25
 */
@Service
public class MajorServiceImpl extends ServiceImpl<MajorMapper, Major> implements MajorService {

    @Override
    public Page<Major> getMajorPage(Page<Major> page, Long schoolId, String name, String category) {
        LambdaQueryWrapper<Major> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(schoolId != null, Major::getSchoolId, schoolId)
                .like(name != null && !name.trim().isEmpty(), Major::getName, name)
                .eq(category != null && !category.trim().isEmpty(), Major::getCategory, category)
                .eq(Major::getDeleted, 0)
                .orderByDesc(Major::getCreatedAt);
        
        return this.page(page, wrapper);
    }

    @Override
    public List<Major> getMajorsBySchoolId(Long schoolId) {
        return baseMapper.selectBySchoolId(schoolId);
    }

    @Override
    public List<String> getCategories() {
        return baseMapper.selectCategories();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createMajor(Major major) {
        // 设置默认值
        if (major.getStatus() == null) {
            major.setStatus(1);
        }
        if (major.getStudyYears() == null) {
            major.setStudyYears(3);
        }
        
        return this.save(major);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateMajor(Major major) {
        return this.updateById(major);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteMajor(Long id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteMajorBatch(List<Long> ids) {
        return this.removeByIds(ids);
    }
}